﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Resources;
using System.Xml;

namespace Songhay.Silverlight.ApplicationLoader
{
    using Models;

    /// <summary>
    /// Static members for <see cref="System.Windows.Deployment"/>.
    /// </summary>
    internal static class DeploymentUtility
    {
        /// <summary>
        /// Gets the external deployment reader.
        /// </summary>
        /// <param name="e">The <see cref="System.Net.OpenReadCompletedEventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// Silverlight defines the <see cref="System.Windows.Deployment"/> as a singleton
        /// for the entire application domain. It follows that we use the
        /// <see cref="System.Xml.XmlReader"/> as the lightest way to get
        /// external deplpoyment data.
        /// </remarks>
        internal static XmlReader GetExternalDeploymentReader(OpenReadCompletedEventArgs e)
        {
            var xapInfo = new StreamResourceInfo(e.Result, null);

            var manifestInfo = Application.GetResourceStream(xapInfo,
                new Uri("AppManifest.xaml", UriKind.Relative));

            return XmlReader.Create(new StreamReader(manifestInfo.Stream));
        }

        /// <summary>
        /// Gets the manifest source.
        /// </summary>
        /// <param name="deployment">The deployment.</param>
        internal static string GetManifestSource(XmlReader deployment)
        {
            deployment.MoveToAttribute("Source");
            deployment.ReadAttributeValue();
            return deployment.Value;
        }

        /// <summary>
        /// Lists the deployment assembly parts.
        /// </summary>
        /// <param name="e">The <see cref="System.Net.OpenReadCompletedEventArgs"/> instance containing the event data.</param>
        internal static IEnumerable<DeploymentAssemblyPart> ListDeploymentAssemblyParts(OpenReadCompletedEventArgs e)
        {
            var deployment = DeploymentUtility.GetExternalDeploymentReader(e);
            return DeploymentUtility.ListDeploymentAssemblyParts(deployment);
        }

        /// <summary>
        /// Lists the deployment assembly parts.
        /// </summary>
        /// <param name="deployment">The deployment.</param>
        internal static IEnumerable<DeploymentAssemblyPart> ListDeploymentAssemblyParts(XmlReader deployment)
        {
            if(deployment == null) throw new ArgumentNullException("deployment", "The Deployment Reader is null.");

            var list = new List<DeploymentAssemblyPart>();

            while(deployment.Read())
            {
                if(!deployment.IsStartElement("AssemblyPart")) continue;

                var source = GetManifestSource(deployment);
                list.Add(new DeploymentAssemblyPart{Source = source});
            }

            return list;
        }
    }
}
